import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import org.springframework.beans.support.PagedListHolder;

public class StatusBar extends JPanel implements ActionListener {

	JFrame frame = new JFrame("分页sample");

	Integer[] pageSizeArr = new Integer[] { 10, 50, 100 };
	JButton previous;
	JButton next;
	JLabel statusLabel;
	JTextField pageNoText;
	private final static int H_BUT_PAD = 5;
	private PagedListHolder pagedListHolder;

	JLabel msg = new JLabel("ddddddd");

	public StatusBar(PagedListHolder pagedListHolder) {
		super();
		this.pagedListHolder = pagedListHolder;

		this.frame.setBounds(100, 100, 1024, 768);

		this.setBorder(BorderFactory.createEmptyBorder(3, 0, 0, 0));
		this.setLayout(new BorderLayout());
		Box statusBox = Box.createHorizontalBox();

		// Left status area
		statusBox.add(Box.createRigidArea(new Dimension(10, 22)));
		previous = new JButton("previous");
		next = new JButton("next");
		previous.addActionListener(this);
		next.addActionListener(this);

		statusLabel = new JLabel("<html>" + pagedListHolder.getPageCount()
				+ "件中  " + (pagedListHolder.getPage() + 1) + "&nbsp;件目</html>");

		statusBox.add(statusLabel);
		statusBox.add(Box.createHorizontalGlue());
		statusBox.add(previous);
		statusBox.add(Box.createHorizontalStrut(H_BUT_PAD));
		statusBox.add(next);

		statusBox.add(msg);

		this.add(statusBox);

		initButtonStatus();

	}

	public StatusBar() {
		super();

		initPagedListHolder();

		this.frame.setBounds(100, 100, 1024, 768);

		this.setBorder(BorderFactory.createEmptyBorder(3, 0, 0, 0));
		this.setLayout(new BorderLayout());
		Box statusBox = Box.createHorizontalBox();

		// Left status area
		statusBox.add(Box.createRigidArea(new Dimension(10, 22)));
		previous = new JButton("previous");
		// previous.setText("上一页");
		next = new JButton("next");
		previous.addActionListener(this);
		next.addActionListener(this);

		statusLabel = new JLabel("<html>" + pagedListHolder.getPageCount()
				+ "件中  " + (pagedListHolder.getPage()+1) + "&nbsp;件目</html>");
		statusBox.add(statusLabel);
		statusBox.add(Box.createHorizontalGlue());
		statusBox.add(previous);
		statusBox.add(Box.createHorizontalStrut(H_BUT_PAD));
		statusBox.add(next);
		statusBox.add(msg);

		this.add(statusBox);

		initButtonStatus();

	}

	/*
	 * 设置按钮状态
	 */

	private void initButtonStatus() {

		previous.setEnabled(!pagedListHolder.isFirstPage());
		next.setEnabled(!pagedListHolder.isLastPage());
	}

	// final public void loadData() {
	// EventQueue.invokeLater(new Runnable() {
	//
	// public void run() {
	// loading();
	//
	// }
	// });
	// }

	/**
	 * 开始查询加载 pagedListHolder.setQuerySQL(sql);
	 * pagedListHolder.setTotalSize(BaseDAO
	 * .getInstance().count(pagedListHolder.getCountSQL()));
	 * pagedListHolder.setCurSize
	 * (BaseDAO.getInstance().count(pagedListHolder.getCurCountSQL()));
	 */
	final private void loading() {
		// initPagedListHolder();
		query();
	}

	/**
	 * 设置pagedListHolder
	 */
	final private void initPagedListHolder() {

		ArrayList<String> list = new ArrayList<String>();

		// 初始化list实例
		for (int i = 1; i < 11; i++) {
			list.add("num" + i);
		}
		this.pagedListHolder = new PagedListHolder<String>(list);

		pagedListHolder.setPageSize(1);
	}

	/**
	 * 填充表格 可 重写
	 */
	public void fillToTable() {
		String str = "";
		Iterator it = pagedListHolder.getPageList().iterator(); // 获得当前页记录的一个list，是整个list的一个子list
		// 显示每页的内容
		while (it.hasNext()) {
			str += it.next().toString();
		}
		this.msg.setText(str);
	}

	/*
	 * 查询
	 */

	final private void query() {
		fillToTable();
		finished();
	}

	/*
	 * 查询完成
	 */

	final private void finished() {
		initButtonStatus();
		// this.statusLabel = new JLabel("<html>" +
		// pagedListHolder.getPageCount()
		// + "件中  " + (pagedListHolder.getPage() + 1)+ "&nbsp;件目</html>");
		this.statusLabel.setText("<html>" + pagedListHolder.getPageCount()
				+ "件中  " + (pagedListHolder.getPage() + 1) + "&nbsp;件目</html>");

	}

	public void setPagedListHolder(PagedListHolder pagedListHolder) {
		this.pagedListHolder = pagedListHolder;
	}

	public PagedListHolder getPagedListHolder() {
		return pagedListHolder;
	}

	public void actionPerformed(ActionEvent ae) {
		String command = ae.getActionCommand();
		if (command.equals("next")) {
			pagedListHolder.nextPage();
		} else if (command.equals("previous")) {
			pagedListHolder.previousPage();
		}

		if (pagedListHolder != null) {
			this.query();
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		StatusBar statusBar = new StatusBar();
		statusBar.loading();
		try {
			// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			UIManager.setLookAndFeel(UIManager
					.getCrossPlatformLookAndFeelClassName());
		} catch (Exception exc) {
			System.err.println("Error loading L&F: " + exc);
		}

		statusBar.frame.addWindowListener(new WindowAdapter() {

			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		// frame.getContentPane().add("Center", panel);
		statusBar.frame.getContentPane().add(statusBar, BorderLayout.CENTER);
		statusBar.frame.pack();
		statusBar.frame.setVisible(true);

	}

}
